package com.testgame;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class TestgameActivity extends Activity implements SurfaceHolder.Callback,Runnable{
    /** Called when the activity is first created. */
	private final static int blockweight = 34;
	
	private int mWidth;
	private int mHeight;
	private SurfaceHolder mHolder;
	private Paint mPaint = new Paint();
	private Bitmap bitmap;
	
	private boolean bool;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN , WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.main);
        
        SurfaceView sv = (SurfaceView)findViewById(R.id.gamebackgroud);
        sv.getHolder().addCallback(this);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.redstar);
//        mPaint.setColor(Color.RED);
    }

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
			mWidth = width;
			mHeight = height;
			mHolder = holder;
			
			initBack();
			initSnake();
			getValue();
			
			Thread thread = new Thread(this);
			thread.start();
			bool = true;
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		bool = false;
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Canvas canvas = null;
		int i = 0;
			while(bool){
				try{
					
					canvas  = mHolder.lockCanvas(null);
					drawBackgroud(canvas);
					drawOne(canvas);
					drawSnake(canvas);
				}catch (Exception e) {
					e.printStackTrace();
				}finally{
					if(canvas != null){
						mHolder.unlockCanvasAndPost(canvas);
					}
				}
				
				try {
					Thread.sleep(400);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
	}
	
	
	private int up_down;
	private int up_down_count;
	private int left_right;
	private int left_right_count;
	
	private int updownright;
	private int leftrightdown;
	private void initBack(){
			up_down_count = mHeight/blockweight;
			up_down = (mHeight-up_down_count*blockweight)/2;
			left_right_count = mWidth/blockweight;
			left_right = (mWidth-left_right_count*blockweight)/2;
			updownright = mWidth-left_right-blockweight;
			leftrightdown = mHeight-up_down-blockweight;
	}
	
	private void drawBackgroud(Canvas canvas){
			canvas.drawColor(Color.BLACK);
			
			int updown = up_down;
			for(int i=0; i<up_down_count; i++){
					canvas.drawBitmap(bitmap, left_right, updown, mPaint);
					canvas.drawBitmap(bitmap,updownright, updown, mPaint);
					updown += blockweight;
			}
			
			int leftright = left_right+blockweight;
			for(int i=0; i<left_right_count-2; i++){
					canvas.drawBitmap(bitmap, leftright, up_down, mPaint);
					canvas.drawBitmap(bitmap,leftright,leftrightdown, mPaint);
					leftright += blockweight;
			}
	}
	
	
	private void initSnake(){
			int centerx = left_right_count/2;
			int centery = up_down_count/2;
			
			list.addFirst(new Snake(centerx, centery));
			list.addFirst(new Snake(centerx-1, centery));
			list.addFirst(new Snake(centerx-2, centery));
	}
	
	private LinkedList<Snake> list = new LinkedList<Snake>();
	private void drawSnake(Canvas canvas){
			for(int i=0; i<list.size(); i++){
					Snake tempsnake = list.get(i);
					canvas.drawBitmap(bitmap, tempsnake.getX()*blockweight+left_right, tempsnake.getY()*blockweight+up_down, mPaint);
					
					if(i==list.size()-1){
						if(tempsnake.getX()==ranx&&tempsnake.getY()==rany){
							int tempx = list.get(0).getX();
							int tempy = list.get(0).getY();
							switch (fanxiang) {
							case 1:
								tempx+=1;
								break;
							case 2:
								tempx-=1;
								break;
							case 3:
								tempx-=1;
								break;
							case 4:
								tempx+=1;
								break;
							default:
								break;
							}
							list.addFirst(new Snake(tempx, tempy));
							getValue();
						}
						
						
						
						if(tempsnake.getX()==left_right_count||tempsnake.getY()==up_down_count||tempsnake.getX()==0||tempsnake.getX()==0){
							finish();
						}
						
						switch (fanxiang) {
						case 1:
							tempsnake.setY(tempsnake.getY()-1);
							break;
						case 2:
							tempsnake.setY(tempsnake.getY()+1);
							break;
						case 3:
							tempsnake.setX(tempsnake.getX()-1);
							break;
						case 4:
							tempsnake.setX(tempsnake.getX()+1);
							break;
						default:
							break;
						}
						
					}else{
						Snake oldsnake = list.get(i+1);
						tempsnake.setX(oldsnake.getX());
						tempsnake.setY(oldsnake.getY());
					}
			}
	}
	
	private Random randomx = new Random();
	private Random randomy = new Random();
	
	private int ranx;
	private int rany;
	private void drawOne(Canvas canvas){
			canvas.drawBitmap(bitmap,ranx*blockweight+left_right,rany*blockweight+up_down, mPaint);
	}
	
	private void getValue(){
			ranx = randomx.nextInt(left_right_count-1);
			ranx++;
			rany = randomy.nextInt(up_down_count-1);
			rany++;
	}
	
	
	private int fanxiang = 4;
	public void change(View v){
			if(v.getTag().equals("up")){
				fanxiang = 1;
			}else if(v.getTag().equals("down")){
				fanxiang = 2;
			}else if(v.getTag().equals("left")){
				fanxiang = 3;
			}else if(v.getTag().equals("right")){
				fanxiang = 4;
			}
	}
}